
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: ActionInterceptor.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.aop;

import java.util.concurrent.Callable;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.dellroad.sidekar.util.CloseableNotes;

/**
 * Method interceptor that wraps the method invocation inside a call to {@link CloseableNotes#performAction}.
 */
public class ActionInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        return CloseableNotes.performAction(new Callable<Object>() {
            public Object call() throws Exception {
                try {
                    return invocation.proceed();
                } catch (RuntimeException e) {
                    throw e;
                } catch (Exception e) {
                    throw e;
                } catch (Error e) {
                    throw e;
                } catch (Throwable t) {
                    throw new RuntimeException(t);
                }
            }
        });
    }
}

